package web.controller.sys;

import com.github.pagehelper.PageInfo;
import framework.config.UserConfig;
import framework.utils.RequestUtil;
import framework.vo.R;
import framework.vo.ResultInfo;
import framework.vo.ResultList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import system.entity.SysRole;
import system.entity.SysRolePermission;
import system.entity.SysRoleUser;
import system.service.sys.SysRolePermissionService;
import system.service.sys.SysRoleService;
import system.service.sys.SysRoleUserService;

import java.util.List;

@RestController
@RequestMapping("/sys/role")
@Api(tags = "角色权限")
public class SysRoleController {
    @Autowired
    private UserConfig userConfig;
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysRoleUserService sysRoleUserService;
    @Autowired
    private SysRolePermissionService sysRolePermissionService;

    @ApiOperation("添加角色")
    @PostMapping("/add")
    public ResultInfo<Integer> add(@ModelAttribute SysRole param) {
        java.util.Date now = new java.util.Date();
        param.setCreateTime(now);
        param.setUpdateTime(now);
        Integer added = sysRoleService.add(param);
        ResultInfo<Integer> info = new ResultInfo<>(param.getId());
        return info;
    }

    @ApiOperation("删除角色")
    @PostMapping("/del")
    public ResultInfo<Integer> del(@RequestParam("ids") List<Integer> ids) {
        for (Integer id : ids) {
            if (id == this.userConfig.getAdminRoleId()) {
                SysRole sysRole = sysRoleService.loadById(id);
                String n = sysRole != null ? sysRole.getName() : id.toString();
                String m = RequestUtil.getMessageDefault("deny.delete.system.object", "{0} is system object，not allow remove", n);
                return R.failed(m);
            }
            //
            SysRoleUser roleUser = new SysRoleUser();
            roleUser.setRoleId(id);
            SysRoleUser u = sysRoleUserService.first(roleUser);
            if (u != null) {
                SysRole sysRole = sysRoleService.loadById(id);
                String n = sysRole != null ? sysRole.getName() : id.toString();
                String m = RequestUtil.getMessageDefault("role.delete.have.children", "Role {0} to be deleted also contains users. remove user in the role and try again", n);
                return R.failed(m);
            }
        }

        Integer deleted = sysRoleService.deleteByIds(ids);
        ResultInfo<Integer> info = new ResultInfo<>(deleted);
        return info;
    }

    @ApiOperation("更新角色")
    @PostMapping("/edit")
    public ResultInfo<Integer> edit(@ModelAttribute SysRole param) {
        java.util.Date now = new java.util.Date();
        param.setUpdateTime(now);
        Integer updated = sysRoleService.update(param);
        ResultInfo<Integer> info = new ResultInfo<>(updated);
        return info;
    }

    @ApiOperation("更新角色权限")
    @PostMapping("/permission/save")
    public ResultInfo<Integer> permissionSave(@RequestParam("id") Integer id, @RequestParam(value = "permissionIds", required = false) String permissionIds) {
        java.util.Date now = new java.util.Date();
        Integer updated;

        if (StringUtils.isEmpty(permissionIds)) {
            permissionIds = "";
        }

        String[] permissionList = permissionIds.split(",");
        updated = this.sysRolePermissionService.updateRolePermissions(id, permissionList);

        ResultInfo<Integer> info = new ResultInfo<>(updated);
        return info;
    }

    @ApiOperation("角色权限加载")
    @GetMapping("/permission/list")
    public ResultList<SysRolePermission> permissionList(SysRolePermission param, @RequestParam("pageIndex") Integer pageIndex, @RequestParam("pageSize") Integer pageSize) {
        PageInfo<SysRolePermission> info = sysRolePermissionService.list(param, pageIndex, pageSize);
        return new ResultList<>(info);
    }

    @ApiOperation("查看角色")
    @GetMapping("/info")
    public ResultInfo<SysRole> info(@RequestParam("id") Integer id) {
        SysRole info = sysRoleService.loadById(id);
        return new ResultInfo<>(info);
    }

    @ApiOperation("角色列表")
    @GetMapping("/list")
    public ResultList<SysRole> list(SysRole param, @RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageInfo<SysRole> info = sysRoleService.list(param, pageIndex, pageSize);
        return new ResultList<>(info);
    }
}